﻿using System;
using System.Collections.Generic;
using System.Web;
using VCN.App.DataTransferObjects;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Security.Cryptography;

namespace VCN.App.DataAccessLayer
{
    public class UsersDAO:BasicDAO<Users>
    {
        //public override List<Users> SelectFilter2List(Users ip_obj, int ip_id, string ip_elang)
        //{
        //    return null;
        //}

        //public override List<Users> exeConvert(DataTable ip_dt)
        //{
        //    throw new NotImplementedException();
        //}
        public bool Insert(Users ip_obj)
        {
            if (ExecuteNonQuery("pr_tblUser_Insert", "@Username", ip_obj.UserName, "@Password", ip_obj.Password, "@Role", ip_obj.UserLevel) > 0)
                return true;
            else
                return false;

        }
        public bool GetLogin(Users ip_user)
        {
           DataTable op_dt = new DataTable();
           op_dt = GetData("pr_tblcomone", "@UserName", ip_user.UserName, "@Password", ip_user.Password);
           return (op_dt.Rows.Count > 0);            
        }
        public bool CheckRole(string ip_str_UserName)
        {
            DataTable v_dt_result = new DataTable();
            v_dt_result = GetData("pr_tblUser_Get_Role_From_Username", "@UserName", ip_str_UserName);
            if (int.Parse(v_dt_result.Rows[0]["Role"].ToString()) == 2)
                return false;
            return true;
        }
        public DataTable GetAdminName()
        {
            return this.exeSpSelect("sp_tblUsers_SelectAdminName");
        }
        public int GetUIdFromUserName(string ip_str_userName)
        {
            DataTable v_dt_user = new DataTable();
            v_dt_user = GetData("sp_tblUsers_SelectIdByUserName", "@UserName", ip_str_userName);
            return int.Parse(v_dt_user.Rows[0]["ID"].ToString());
        }
        public string Encrypt(string toEncrypt, string key, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        public string Decrypt(string toDecrypt, string key, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }
        public bool check_exist_user(string ip_str_user)
        {
            DataTable v_dt = new DataTable();
            v_dt = GetData("pr_tblUser_Check_Exist_User", "@UserName", ip_str_user);
            if (v_dt.Rows.Count > 0) return true; // Đã tồn tại
            return false; // chưa tồn tại
        }
        public bool UnlockUser(int ip_i_UId)
        {
            if (ExecuteNonQuery("sp_tblUser_LockAndUnlock", "@Role", 1, "@UserId", ip_i_UId) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool LockUser(int ip_i_UId)
        {
            if (ExecuteNonQuery("sp_tblUser_LockAndUnlock", "@Role", 2, "@UserId", ip_i_UId) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public DataTable getAllUsersRoleOne(string ip_elang)
        {
            return this.exeSpSelect("sp_tblusers_selectAllUsersRoleOne");
        }
        public bool changePass(Users ip_user)
        {
            if (ExecuteNonQuery("pr_tblUser_changePass","@NewPass", ip_user.Password,"@UserId", ip_user.Id) > 0) return true;
            return false;
        }
        public int get_id_from_user_name(string ip_str_username)
        {
            DataTable m_dt_comm = new DataTable();
            m_dt_comm =  GetData("pr_tblUser_Get_Id_From_Username", "@UserName", ip_str_username);
            if (m_dt_comm.Rows.Count > 0)
                return Convert.ToInt32(m_dt_comm.Rows[0]["Id"]);
            return -1;
        }
        //public override bool Update(params Users[] ip_arr_obj)
        //{
        //    throw new NotImplementedException();
        //}

        //public override bool Insert(params Users[] ip_arr_obj)
        //{
        //    throw new NotImplementedException();
        //}
        public DataTable getAllUsersAcc()
        {
            DataTable op_dt = new DataTable();
            op_dt = GetData("pr_tblUser_SelectFilter");
            return op_dt;
        }

        public override List<Users> selectAll2List(Users ip_obj)
        {
            throw new NotImplementedException();
        }

        public override bool update(Users ip_obj)
        {
            throw new NotImplementedException();
        }

        public override bool insert(Users ip_obj)
        {
            throw new NotImplementedException();
        }
        protected override DataTable exeSpSelect(string ip_storedProcedure)
        {
            return base.exeSpSelect("sp_tblUsers_SelectAll");//sp_tblCategories_SelectAll
        }
        public DataTable selectAll2Dt()
        {
            return this.exeSpSelect("");
        }
        protected DataTable exeSpSelectById(string ip_storeProcedure, int ip_id)
        {
            return base.exeSpSelectById("sp_tblUsers_SelectRow", ip_id);
        }
        public Users SelectById(int ip_id)
        {
            DataTable v_dt = this.exeSpSelectById("", ip_id);
            Users v_Users = new Users();
            v_Users.Id = ip_id;
            v_Users.UserName = v_dt.Rows[0]["USERNAME"].ToString();
            return v_Users;
        }
    }
}
